package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.metric.MetricDefineEntity;
import com.hllxd.hism.star.basic.service.model.UserFocusMetricEntity;
import com.hllxd.hism.star.basic.service.service.MetricDefineService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 计算配额管理
 *
 * @author xiyong.lxy
 * @date 2020-10-12 16:51
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/metric_define")
public class MetricDefineController2 {

    @Autowired
    private MetricDefineService metricDefineService;

    @Data
    public static class Param {
        public Long id;
        public String ids;
        public String phoneNumber;
        public String bizArea;
        public String topic;

        public String dt;
        public String dt2;

        public String search;
        public String metricId;
    }

    /**
     * 指标分组路由获取
     * */

    @RestGrpcMapping("/listMetricRouter.do")
    public NTSTATUS listMetricRouter(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            List<MetricDefineEntity> items = metricDefineService.listMetricRouter(aa.bizArea);
            log.info("items: {}.", items.size());
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    /**
     * 指标列表
     * */

    @RestGrpcMapping("/listMetric.do")
    public NTSTATUS listMetric(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            List<MetricDefineEntity> items = metricDefineService.listMetric(aa.bizArea, aa.topic);
            log.info("items: {}.", items.size());
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    /**
     * 指标管理
     * */

    @RestGrpcMapping("/searchMetric.do")
    public NTSTATUS searchMetric(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            List<MetricDefineEntity> items = metricDefineService.searchMetric(aa.bizArea,aa.search);
            log.info("items: {}.", items.size());
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }
    /**
     * 指标管理
     * */
    @RestGrpcMapping("/getMetric.do")
    public NTSTATUS getMetric(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            MetricDefineEntity item = metricDefineService.getMetric(aa.id);
            result.setData(item);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    /**
     * 指标管理
     * */
    @RestGrpcMapping("/setMetric.do")
    public NTSTATUS setMetric(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            MetricDefineEntity aa = params.driverObject(MetricDefineEntity.class);
            Object res = metricDefineService.setMetric(aa);
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    /**
     * 指标管理
     * */
    @RestGrpcMapping("/deleteMetric.do")
    public NTSTATUS deleteMetric(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            Boolean ret = metricDefineService.deleteMetric(aa.ids);
            result.setData(ret);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    /**
     * 指标订阅管理
     * */
    @RestGrpcMapping("/subscribe.do")
    public NTSTATUS subscribe(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            long res = metricDefineService.focusMetric(params.userID, aa.getId());
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }
    /**
     * 指标订阅管理
     * */
    @RestGrpcMapping("/cancelSubscribe.do")
    public NTSTATUS cancelSubscribe(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            long res = metricDefineService.cancelSubscribe(params.userID, aa.getId());
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    /**
     * 指标订阅管理
     * */
    @RestGrpcMapping("/listUserSubscribe.do")
    public NTSTATUS listUserSubscribe(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            List<UserFocusMetricEntity> res = metricDefineService.listUserSubscribe(params.userID);
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }


}
